package com.yswee.mallnavi.view.brand;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.yswee.framework.common.cache.image.ImageWorker;
import com.yswee.framework.utils.StringUtils;
import com.yswee.framework.utils.ViewInject;
import com.yswee.framework.widget.clickedtab.ClickedTabBar;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.entity.info.CategoryEntity;

import java.util.ArrayList;


/***
 * TabBar
 */
public class TabBar extends ClickedTabBar {

    @ViewInject
    private LinearLayout llcontent;

    public TabBar(Context context) {
        super(context);
    }

    public TabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.view_info_brand_tabbar;
    }

    @Override
    protected void onBindListener() {
        super.onBindListener();
    }

    @Override
    protected int[] getChildViewResIDs() {
        return new int[] {
        };
    }

    public void setDataArray(ArrayList<CategoryEntity> array) {
        llcontent.removeAllViews();
        if(array != null && array.size() > 0) {
            childviewList = new View[array.size()];
            for(int i = 0; i < array.size(); i++) {
                CategoryEntity category = array.get(i);
                LinearLayout lltabitem = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.view_info_brand_tabbar_item, null, false);
                TextView textview = ((TextView) lltabitem.getChildAt(1));
                textview.setText(category.name);
                ImageView imgpicture = (ImageView) lltabitem.getChildAt(0);
                if (!StringUtils.isEmpty(category.iconUrl) && category.iconUrl.startsWith(ImageWorker.RESFILE_PREFIX)) {
                    int resId = Integer.parseInt(category.iconUrl.substring(ImageWorker.RESFILE_PREFIX.length()));
                    imgpicture.setImageResource(resId);
                } else {
                    imgpicture.setBackgroundResource(R.drawable.default_image_s);
                    Glide.with(this)
                            .load(array.get(i).iconUrl)
                            .addListener(new RequestListener<Drawable>() {
                                @Override
                                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                    return false;
                                }

                                @Override
                                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                    imgpicture.setBackgroundResource(R.color.transparent);
                                    return false;
                                }
                            })
                            .into(imgpicture);
                }
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT
                );
                llcontent.addView(lltabitem, layoutParams);
                childviewList[i] = lltabitem;
                if (lltabitem != null){
                    final int index = i;
                    lltabitem.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            if(null != listener) {
                                listener.onTabChanged(index);
                            }
                            setTabIndex(index);
                        }
                    });
                }
            }
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void focusChildView(int index, View v) {
        TextView textview = (TextView) ((LinearLayout) v).getChildAt(1);
        textview.setTextColor(mContext.getResources().getColor(R.color.widget_tabbar_focus_s_fg));
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void unfocusChildView(int index, View v) {
        TextView textview = (TextView) ((LinearLayout) v).getChildAt(1);
        textview.setTextColor(mContext.getResources().getColor(R.color.widget_tabbar_unfocus_fg));
    }

}